﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cloud : MonoBehaviour {

    /// <summary>
    /// 最大移动速度
    /// </summary>
    [Tooltip("最大移动速度"),SerializeField]
    private float MaxSpeed = 10f;

    private float speed;

    private float nowTime = 0f;

    [Tooltip("用于控制时间的倍数"), SerializeField]
    private float multipleValue = 10f;

    [Tooltip("可更换的图片"), SerializeField]
    private Sprite[] sprites;

    [Tooltip("到了左边缘后返回的位置"),SerializeField]
    private Transform CreatTrans;

    private void FixedUpdate()
    {
        Vector2 newPosition = transform.position;
        newPosition.x -= speed * Time.deltaTime *
            Mathf.Sin(nowTime*Mathf.PI/multipleValue);
        transform.position = newPosition;
        nowTime = (nowTime + Time.deltaTime) % multipleValue;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.collider.tag==Tag.leftBorder)
        {
            Vector2 newPosition = transform.position;
            newPosition.x = CreatTrans.position.x;
            transform.position = newPosition;
            int randomValue = (int)((GameManager.Instance.GetRandomValue(0,1f) * 100));
            GetComponent<SpriteRenderer>().sprite =
                sprites[randomValue % sprites.Length];
            speed = MaxSpeed * GameManager.Instance.GetRandomValue(0.5f, 1f);
        }
    }

    private void Start()
    {
        int randomValue = (int)((GameManager.Instance.GetRandomValue(0, 1f) * 100));
        GetComponent<SpriteRenderer>().sprite =
                   sprites[randomValue % sprites.Length];
        speed = MaxSpeed * GameManager.Instance.GetRandomValue(0.5f, 1f);
        nowTime = GameManager.Instance.GetRandomValue(0, 1f);
    }

}
